package com.wuhao.lib_common.http;

import android.util.Log;


import com.wuhao.lib_common.bean.ResultBean;
import com.wuhao.lib_common.configure.HttpConfig;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Created by zhangjianhao on 2018/6/12.
 */

public abstract class AbstractHttpSubscriber<T> implements Observer<ResultBean<T>> {
    ResultBean resultBean;

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        Log.e("AbstractHttpSubscriber", "onSubscribe");
    }

    @Override
    public void onNext(@NonNull ResultBean<T> tResultBean) {
        try {
            if (Integer.valueOf(tResultBean.getCode()).intValue() == HttpConfig.RESULT_OK) {
                success(tResultBean);
            } else {
                fail(tResultBean);
            }
        } catch (Exception e) {
            onError(e);
        }
    }

    @Override
    public void onComplete() {
        Log.e("AbstractHttpSubscriber", "onComplete");
    }

    @Override
    public void onError(Throwable e) {
        Log.e("AbstractHttpSubscriber:", e.getMessage() + "");
        if (e != null) {
            error(e);
        }
    }

    public abstract void success(ResultBean<T> completeT);

    public abstract void fail(ResultBean<T> completeT);

    public abstract void error(Throwable e);


    public AbstractHttpSubscriber() {
    }
}